package com.lesso.zgs.sunflower.core.sharding;

import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;

import java.util.ArrayList;
import java.util.Collection;

/**
 * YearRangeShardingAlgorithm
 * 按年份分表的范围分片算法
 */
public class YearRangeShardingAlgorithm implements RangeShardingAlgorithm<String> {
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<String> shardingValue) {
        Collection<String> result = new ArrayList<>();
        String startTimestamp = shardingValue.getValueRange().lowerEndpoint();
        String endTimestamp = shardingValue.getValueRange().upperEndpoint();
        int startYear = Integer.parseInt(startTimestamp.substring(0, 4));
        int endYear = Integer.parseInt(endTimestamp.substring(0, 4));
        for (int year = startYear; year <= endYear; year++) {
            String targetTableName = "base_employee_" + year;
            for (String tableName : availableTargetNames) {
                if (tableName.equals(targetTableName)) {
                    result.add(tableName);
                    break;
                }
            }
        }
        return result;
    }
} 